package cn.xopencode.recordservice.service.roomin;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.roomin.RoomInRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.roomin.RoomInRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.roomin.RoomInRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.roomin.RoomInRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.roomin.RoomInRecordMapper;
import cn.xopencode.recordservice.rpc.roomin.dto.AdminRoomInRecordVO;
import cn.xopencode.recordservice.rpc.roomin.vo.RoomInRecordPageVO;
import cn.xopencode.recordservice.rpc.roomin.vo.RoomInRecordUpdateVO;
import cn.xopencode.recordservice.service.roomin.bo.AdminRoomInRecordBO;
import cn.xopencode.recordservice.service.roomin.bo.RoomInRecordCreateBO;
import cn.xopencode.recordservice.service.roomin.bo.RoomInRecordPageBO;
import cn.xopencode.recordservice.service.roomin.bo.RoomInRecordUpdateBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 配电室进出
 */
@Service
@Validated
public class RoomInRecordService {
    @Autowired
    private RoomInRecordMapper roomInRecordMapper;
    @Autowired
    private RoomInRecordEmpRelMapper roomInRecordEmpRelMapper;
    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;

    public Integer createRoomInRecord(RoomInRecordCreateBO createBO, Set<String> empIds){
        RoomInRecordDO recordDO = RoomInRecordConvert.INSTANCE.convert(createBO);
        String recordId = IdUtil.objectId();
        recordDO.setId(recordId);
        Integer result = roomInRecordMapper.insert(recordDO);
        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<RoomInRecordEmpRelDO> roominRecordEmpRels = empIds.stream()
                    .map(empId -> new RoomInRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(recordId).setEmpId(empId)).collect(Collectors.toList());
            roomInRecordEmpRelMapper.insertList(roominRecordEmpRels);
        }
        return result;
    }

    public Integer updateRoomInRecord(RoomInRecordUpdateBO updateBO, Set<String> empIds){
        RoomInRecordDO recordDO = RoomInRecordConvert.INSTANCE.convert(updateBO);
        Integer result = roomInRecordMapper.updateById(recordDO);
        //先删除所有关联的人员
        roomInRecordEmpRelMapper.deleteByRecordId(recordDO.getId());

        //保存关联的进出人员
        if (!CollectionUtil.isEmpty(empIds)) {
            List<RoomInRecordEmpRelDO> roominRecordEmpRels = empIds.stream()
                    .map(empId -> new RoomInRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(recordDO.getId()).setEmpId(empId)).collect(Collectors.toList());
            roomInRecordEmpRelMapper.insertList(roominRecordEmpRels);
        }
        return result;
    }

    /**
     * 根据id获取配电室进出详细记录
     * @param id
     * @return
     */
    public RoomInRecordUpdateVO getRoomInRecordById(String id){
        RoomInRecordDO recordDO = roomInRecordMapper.selectById(id);
        System.out.println(recordDO);
        if(recordDO != null){
            RoomInRecordUpdateVO recordVO = RoomInRecordConvert.INSTANCE.convert(recordDO);
            //查询所有关联的巡检人
            List<RoomInRecordEmpRelDO> empRelDOList = roomInRecordEmpRelMapper.selectListByRecordId(id);
            recordVO.setEmpList(RoomInRecordConvert.INSTANCE.convert(empRelDOList));

            PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(recordDO.getRoomId());
            if(roomDO != null)
                recordVO.setRoomName(roomDO.getRoomName());
            return recordVO;
        }
        return null;
    }

    /**
     * 分页查询
     * @param pageBO
     * @return
     */
    public PageResult<RoomInRecordPageVO> pageRoomInRecord(RoomInRecordPageBO pageBO){
        IPage<RoomInRecordDO> recordDOIPage = roomInRecordMapper.selectPage(pageBO);
        PageResult<RoomInRecordPageVO> recordPageVOPageResult = RoomInRecordConvert.INSTANCE.convert(recordDOIPage);
        if(CollectionUtil.isNotEmpty(recordPageVOPageResult.getList())){
            List<RoomInRecordPageVO> list = recordPageVOPageResult.getList();
            for(RoomInRecordPageVO vo : list){
                List<RoomInRecordEmpRelDO> empRelDOList = roomInRecordEmpRelMapper.selectListByRecordId(vo.getId());
                vo.setEmpIds(StringUtils.join(empRelDOList.stream().map(RoomInRecordEmpRelDO::getEmpId).collect(Collectors.toList()), ","));
            }
        }
        return recordPageVOPageResult;
    }

    /**
     * 后台--根据查询条件 分页获取所有的配电室出入记录
     * @param pageBO
     * @return
     */
    public PageResult<AdminRoomInRecordVO> getAdminPageRoomInRecord(AdminRoomInRecordBO pageBO) {
        Page<RoomInRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if(pageBO.getInDatetime() != null){
            String date = DateUtil.format(pageBO.getInDatetime(), "yyyy-MM-dd");
            pageBO.setInDatetime1(date.concat(" 00:00:00"));
            pageBO.setInDatetime2(date.concat(" 23:59:59"));
        }
        IPage<RoomInRecordDO> recordDOList = roomInRecordMapper.adminQuery(page,pageBO);
        return RoomInRecordConvert.INSTANCE.convert1(recordDOList);
    }

}
